﻿// Autore: Mattia Barrasso

using System;
using System.IO;
using System.Web.UI;

namespace RecipesManager.Data.Operations
{
    public static class ShoppingListPrinter
    {
        public static string ToHtml(this IShoppingList shoppingList)
        {
            var stringWriter = new StringWriter();
	        using (var writer = new HtmlTextWriter(stringWriter))
	        {
                writer.Write(GetDocumentStart());
	            foreach (var shoppingItem in shoppingList)
	            {
	                CreateRow(shoppingItem, writer);
	            }
                writer.Write(GetDocumentEnd());
	        }
            
            return stringWriter.ToString();
        }

        private static void CreateRow(IIngredientQuantity ingredientQuantity, HtmlTextWriter writer)
        {
            writer.RenderBeginTag("tr");
                writer.RenderBeginTag("td");
                    writer.Write(ingredientQuantity.Ingredient.Name);
                writer.RenderEndTag();
                writer.RenderBeginTag("td");
                    writer.Write(ingredientQuantity.FormattedQuantity);
                writer.RenderEndTag();
                writer.RenderBeginTag("td");
                    foreach (var shop in ingredientQuantity.Ingredient.ShopRatings)
                    {
                        writer.AddAttribute("class", shop.Rating == ShopRating.Good ? "negoziPreferiti" : "negozi");
                        writer.RenderBeginTag("div");
                            writer.Write(shop.Name);
                        writer.RenderEndTag();
                    }
                writer.RenderEndTag();
            writer.RenderEndTag();
        }

        private static string GetDocumentStart()
        {
            var currentDate = DateTime.Now.Date.ToShortDateString();
            return "<html><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"/>" +
                   "<head><meta charset=\"utf-8\" />" +
                   "<title>Lista della spesa del " + currentDate + "</title><style>" +
                   ".negozi {}" +
                   ".negoziPreferiti {text-decoration: underline}" +
                   "td,th { border: 1px solid black;padding-bottom: .3em; }" +
                   "#titolo, #sottoTitolo { text-align: center;font: x-large serif;}" +
                   "#sottoTitolo { font: small serif; margin-bottom: 1em; color: darkgray}" +
                   "#footer { border: 1px solid black;float: right;margin-top: 1em;padding: .5em;}" +
                   "</style></head>" +
                   "<body>" +
                   "<div id=\"titolo\">Lista della spesa del "+currentDate+"</div><div id=\"lista\">" +
                   "<div id=\"sottoTitolo\">Stampata con Recipe Manager 1.2, HMSoft</div>"+
                   "<table style=\"width: 100%; text-align: center; border-collapse: collapse;\" >" +
                   "<tr><th>Ingrediente</th><th>Quantità</th><th>Negozi</th></tr>";
        }

        private static string GetDocumentEnd()
        {
            return "</table>" +
                   "<div id=\"footer\">" +
                   "<b>Legenda</b><br/>" +
                   "<div class=\"negoziPreferiti\">Negozio consigliato</div>" +
                   "<div class=\"negozi\">Negozio comune</div>" +
                   "</div>" +
                   "</div>" +
                   "</body>" +
                   "</html>";
        }
    }
}
